pub mod auth;
pub mod code;
pub mod goods;
pub mod psn;
pub mod rule;
pub mod station;
pub mod task;
pub mod tech;
pub mod user;

extern crate serde_json;

use self::serde_json::{json, Value};
use base::TmLocal;
use chrono::prelude::*;
use lazy_static::lazy_static;

lazy_static! {
    static ref client: reqwest::Client = reqwest::Client::new();
}

#[allow(dead_code)]
pub async fn dourl(data: Value, model: &str, cmd: &str) -> Result<Value, Value> {
    let url = format!("http://127.0.0.1:9981/cfgdb/{model}/{cmd}");

    let resp = client
        .post(&url)
        .body(data.to_string())
        .send()
        .await
        .or_else(|e| Err(json!(e.to_string())))?;

    let resp = resp.text().await.or_else(|e| Err(json!(e.to_string())))?;

    serde_json::from_str(&resp).or_else(|e| Err(json!(e.to_string())))
}

#[allow(dead_code)]
pub async fn do_cache_ret(p: String, k: Value, val: Value) {
    let v = json!({"code": 1000, "fail": false, "success": true, "data": val});
    do_ret(p, k, "cache", "show", v).await
}

#[allow(dead_code)]
pub async fn do_cache(p: String, k: Value) {
    let v = e_cache();
    do_ret(p, k, "cache", "show", v).await
}

#[allow(dead_code)]
pub async fn do_node_ret(p: String, k: Value, val: Value) {
    let v = json!({"code": 1000, "fail": false, "success": true, "data": val});
    do_ret(p, k, "node", "show", v).await
}

#[allow(dead_code)]
pub async fn do_node(p: String, k: Value) {
    let v = e_node();
    do_ret(p, k, "node", "show", v).await
}

#[allow(dead_code)]
pub async fn do_ret(p: String, k: Value, a: &str, b: &str, val: Value) {
    let ret = dourl(k, a, b).await.expect(&p);
    assert_eq!(ret, val, "{}", p);
}

#[allow(dead_code)]
pub async fn ok_ret(p: String, k: Value, a: &str, b: &str, val: Value) {
    let v = json!({"code": 1000, "fail": false, "success": true, "data": val});
    do_ret(p, k, a, b, v).await;
}

#[allow(dead_code)]
pub async fn ok(p: String, k: Value, a: &str, b: &str) {
    let ret = dourl(k, a, b).await.expect(&p);
    assert_eq!(
        ret,
        json!({"code": 1000, "fail": false, "success": true}),
        "{}",
        p
    );
}

#[macro_export]
macro_rules! pos {
    () => {
        format!("{}:{}", file!(), line!())
    };
}

#[allow(dead_code)]
pub fn cur_time(fmt: &str) -> String {
    let now: TmLocal = Local::now();
    now.format(fmt).to_string()
}

#[allow(dead_code)]
pub fn e_miss_s(v: &str) -> Value {
    let msg = format!(r#"miss error: "{v}""#);
    json!({"code": 2001, "fail": true, "success": false, "message": msg})
}

#[allow(dead_code)]
pub fn e_miss(v: &str) -> Value {
    let msg = format!(r#"miss or empty error: "{v}""#);
    json!({"code": 2002, "fail": true, "success": false, "message": msg})
}

#[allow(dead_code)]
pub fn e_node() -> Value {
    json!({"code": 2600, "fail": true, "success": false, "message": "rocksdb error: \"value not found\""})
}

#[allow(dead_code)]
pub fn e_cache() -> Value {
    json!({"code": 2700, "fail": true, "success": false, "message": "cache error: \"value not found\""})
}

#[allow(dead_code)]
pub fn e_para(v: &str) -> Value {
    let msg = format!(r#"para error: "{v}""#);
    json!({"code": 2003, "fail": true, "success": false, "message": msg})
}

#[allow(dead_code)]
pub fn e_empty(v: &str) -> Value {
    let msg = format!(r#"data empty error: "{v}""#);
    json!({"code": 2500, "fail": true, "success": false, "message": msg})
}

#[allow(dead_code)]
pub fn e_psn(v: &str) -> Value {
    let msg = format!(r#"psn error: "{v}""#);
    json!({"code": 2504, "fail": true, "success": false, "message": msg})
}

#[allow(dead_code)]
pub fn e_user(v: &str) -> Value {
    let msg = format!(r#"user error: "{v}""#);
    json!({"code": 2800, "fail": true, "success": false, "message": msg})
}

#[allow(dead_code)]
pub fn e_code(v: &str) -> Value {
    let msg = format!(r#"code error: "{v}""#);
    json!({"code": 2505, "fail": true, "success": false, "message": msg})
}
